'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

''' <summary>
''' The client uses Snac0B03 to send a usage report to the server.
''' Sent by: Client
''' Protocoll: AIM, ICQ
''' </summary>
Public Class Snac0B03
    Inherits Snac

    Public Sub New()
        MyBase.New(&HB, &H3)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return TlvUsageReport.CalculateTotalSize
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize

        data.AddRange(TlvUsageReport.Serialize)

        Return data
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        Throw New NotImplementedException
    End Sub

    Private ReadOnly _TlvUsageReport As TlvUsageReport = New TlvUsageReport
    Public ReadOnly Property TlvUsageReport() As TlvUsageReport
        Get
            Return _TlvUsageReport
        End Get
    End Property
End Class

Public Class TlvUsageReport
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H9)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + 2 + 16 + _
         1 + ScreenName.Length + 2 + _
         2 + OperatingSystem.Length + _
         2 + OperatingSystemVersion.ToString.Length + _
         2 + ProcessorType.Length + _
         2 + WinsockDllDescription.Length + _
         2 + WinsockDllVersion.ToString.Length + 12
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize

        '00 09  	   	word  	   	TLV.Type(0x09) - local configuration (?)
        'xx xx 	  	word 	  	TLV.Length
        '00 00 00 00
        '00 00 00 00
        '00 00 00 00
        '00 00 00 00 	  	16 bytes 	  	unknown (zero values)
        'xx 	  	byte 	  	length of the screenname (uin) string
        'xx .. 	  	ascii 	  	screenname (uin) string
        'xx xx 	  	word 	  	unknown (machine class ?)
        'xx xx 	  	word 	  	length of the OS name
        'xx .. 	  	ascii 	  	OS name (ex: Windows 2000)
        'xx xx 	  	word 	  	length of the OS version string
        'xx .. 	  	ascii 	  	OS version (ex: 5.1.2600)
        'xx xx 	  	word 	  	length of the processor type
        'xx .. 	  	ascii 	  	processor type (ex: Intel.Pentium)
        'xx xx 	  	word 	  	length of the winsock description string
        'xx .. 	  	ascii 	  	winsock DLL description string
        'xx xx 	  	word 	  	length of the winsock version string
        'xx .. 	  	ascii 	  	winsock DLL version (ex: 5.1.2600.0)
        '00 00 	  	word 	  	Unknown field
        '00 02 	  	word 	  	Unknown field
        '00 01 	  	word 	  	Unknown field
        '00 01 	  	word 	  	Unknown field
        '00 02 	  	word 	  	Unknown field
        '00 02 	  	word 	  	Unknown field

        data.AddRange(New Byte() {0, 0, 0, 0})

        data.AddRange(ByteConverter.GetBytesForUInt32Date(Date.Now))

        data.AddRange(New Byte() {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingByteLength(ScreenName))

        data.AddRange(New Byte() {0, 0})

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingUInt16Length(OperatingSystem))

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingUInt16Length(OperatingSystemVersion.ToString))

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingUInt16Length(ProcessorType))

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingUInt16Length(WinsockDllDescription))

        data.AddRange(ByteConverter.GetBytesForStringWithLeadingUInt16Length(WinsockDllVersion.ToString))

        data.AddRange(New Byte() {0, 0})
        data.AddRange(New Byte() {0, 2})
        data.AddRange(New Byte() {0, 1})
        data.AddRange(New Byte() {0, 1})
        data.AddRange(New Byte() {0, 2})
        data.AddRange(New Byte() {0, 2})

        Return data
    End Function



    Private _ScreenName As String
    Public Property ScreenName() As String
        Get
            Return _ScreenName
        End Get
        Set(ByVal value As String)
            _ScreenName = value
        End Set
    End Property

    Private _MachineClass As Integer
    Public Property MachineClass() As Integer
        Get
            Return _MachineClass
        End Get
        Set(ByVal value As Integer)
            _MachineClass = value
        End Set
    End Property

    Private _OperatingSystem As String
    Public Property OperatingSystem() As String
        Get
            Return _OperatingSystem
        End Get
        Set(ByVal value As String)
            _OperatingSystem = value
        End Set
    End Property

    Private _OperatingSystemVersion As Version
    Public Property OperatingSystemVersion() As Version
        Get
            Return _OperatingSystemVersion
        End Get
        Set(ByVal value As Version)
            _OperatingSystemVersion = value
        End Set
    End Property

    Private _ProcessorType As String
    Public Property ProcessorType() As String
        Get
            Return _ProcessorType
        End Get
        Set(ByVal value As String)
            _ProcessorType = value
        End Set
    End Property

    Private _WinsockDllDescription As String
    Public Property WinsockDllDescription() As String
        Get
            Return _WinsockDllDescription
        End Get
        Set(ByVal value As String)
            _WinsockDllDescription = value
        End Set
    End Property

    Private _WinsockDllVersion As Version
    Public Property WinsockDllVersion() As Version
        Get
            Return _WinsockDllVersion
        End Get
        Set(ByVal value As Version)
            _WinsockDllVersion = value
        End Set
    End Property
End Class